<template>
  <div class="reviewExperts-container-padding">
    <el-form :inline="true" :model="formInline" class="form-inline">
      <el-form-item label="专家名称：">
        <el-input v-model="formInline.name" />
      </el-form-item>
      <el-form-item label="最高学位：">
        <el-select v-model="formInline.highestDegree" placeholder="请选择">
          <el-option
            v-for="item in selectOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生年月：">
        <el-date-picker
          v-model="formInline.birthDate"
          style="width: 240px"
          type="daterange"
          range-separator="→"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          clearable
        />
      </el-form-item>
      <el-form-item label="专家类别：">
        <el-select v-model="formInline.expertType" placeholder="请选择">
          <el-option
            v-for="item in selectExpertCategory"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主研方向：">
        <el-input v-model="formInline.majorStudyOne" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="margin-right: 10px"
          @click="handSearch"
        >
          搜索
        </el-button>
        <el-button @click="handReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-divider />
    <div class="buttonList">
      <el-button type="primary" @click="handAddPatent">新增评审专家</el-button>
      <el-button :loading="downLoading" @click="handleExport">
        导出搜索结果
      </el-button>
    </div>
    <vxe-grid v-bind="gridOptions" :height="height">
      <template #gender_default="{ row }">
        <span>{{ sexFormater(row.gender) }}</span>
      </template>
      <template #politicalAffiliation_default="{ row }">
        <span>
          {{ politicalAffiliationFormater(row.politicalAffiliation) }}
        </span>
      </template>
      <template #expertType_default="{ row }">
        <span>{{ expertTypeFormater(row.expertType) }}</span>
      </template>
      <template #technicalPosition_default="{ row }">
        <span>{{ technicalPositionFormater(row.technicalPosition) }}</span>
      </template>
      <template #majorStudy="{ row }">
        <span
          v-if="row.majorStudyOne || row.majorStudyTwo || row.majorStudyThree"
        >
          {{ row.majorStudyOne }}、{{ row.majorStudyTwo }}、{{
            row.majorStudyThree
          }}
        </span>
      </template>
      <template #operate="{ row }">
        <el-button type="primary" link @click="viewEvent(row, 1)">
          详情
        </el-button>
        <el-button type="primary" link @click="viewEvent(row, 2)">
          修改
        </el-button>
        <el-button type="danger" link @click="del(row)">删除</el-button>
      </template>
      <template #pager>
        <!--使用 pager 插槽-->
        <vxe-pager
          v-model:current-page="tablePage.currentPage"
          v-model:page-size="tablePage.pageSize"
          :layouts="[
            'Sizes',
            'PrevJump',
            'PrevPage',
            'Number',
            'NextPage',
            'NextJump',
            'FullJump',
            'Total',
          ]"
          :total="tablePage.total"
          @page-change="handlePageChange"
        />
      </template>
    </vxe-grid>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import {
    gridOptions,
    selectOptions,
    selectExpertCategory,
    politicalStatus,
    selectMajorOptions,
    politicalAffiliationFormater,
    expertTypeFormater,
    technicalPositionFormater,
  } from './table/column'
  import { useRouter } from 'vue-router'
  import {
    getReviewsList,
    deleteReview,
    getReviewExport,
  } from '@/api/reviewExperts'
  import { VxePagerEvents } from 'vxe-table'
  import { useDownload } from '@/utils/useDownload'
  import { useDictStore } from '@/store/modules/dict'
  const dictStore = useDictStore()
  const sexList = dictStore.getDictType('system_user_sex')
  const sexFormater = (sex: number | string): string => {
    return sexList.filter((item) => item.value == sex)[0]?.label
  }

  const router = useRouter()
  // 搜索表单
  const formInline = ref({
    name: '',
    highestDegree: '',
    birthDate: '',
    expertType: '',
    majorStudyOne: '',
  })
  // 搜索
  const handSearch = () => {
    getList()
  }
  // 重置搜索
  const handReset = () => {
    formInline.value = {
      name: '',
      highestDegree: '',
      birthDate: '',
      expertType: '',
      majorStudyOne: '',
    }
    getList()
  }

  const getList = () => {
    const params = {
      pageNo: tablePage.currentPage,
      pageSize: tablePage.pageSize,
      ...formInline.value,
    }
    getReviewsList(params).then((res) => {
      const { data } = res
      gridOptions.data = data.list
      tablePage.total = data.total
    })
  }

  // 导出搜索结果
  const { downLoading, downloadFile } = useDownload(
    getReviewExport,
    '评审专家.xls'
  )

  const handleExport = async () => {
    await downloadFile(formInline.value)
  }

  onMounted(() => {
    getList()
  })

  const handAddPatent = () => {
    router.push('/reviewExperts/add')
  }

  const viewEvent = (row: any, type: number) => {
    if (type == 1) {
      router.push({ path: '/reviewExperts/detail', query: { id: row.id } })
    } else if (type == 2) {
      router.push({ path: '/reviewExperts/add', query: { id: row.id } })
    }
  }

  const tablePage = reactive({
    total: 0,
    currentPage: 1,
    pageSize: 10,
  })

  const handlePageChange: VxePagerEvents.PageChange = ({
    currentPage,
    pageSize,
  }) => {
    tablePage.currentPage = currentPage
    tablePage.pageSize = pageSize
    getList()
  }

  const del = (row: any) => {
    ElMessageBox.confirm('确定删除该条数据?', '删除', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      deleteReview(row.id).then((res) => {
        getList()
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
      })
    })
  }
  const height = ref(0)
  interface BaseTableHeight {
    (padding: number): number
  }
  const $baseTableHeight = inject<BaseTableHeight>('$baseTableHeight')
  onBeforeMount(() => {
    window.addEventListener('resize', handleHeight)
  })

  onBeforeUnmount(() => {
    window.removeEventListener('resize', handleHeight)
  })

  const handleHeight = () => {
    height.value = $baseTableHeight(0)
  }
</script>

<style scoped lang="scss">
  .buttonList {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  :deep(.el-divider) {
    margin: 8px 0 20px 0 !important;
  }

  .form-inline {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-column-gap: 20px;
  }
</style>
